package com.giggle.nacos.discovery;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.ribbon.NacosServer;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.giggle.common.helper.ReflectHelper;
import com.netflix.client.config.IClientConfig;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author guozichen
 * @ClassName:
 * @Description: (这里用一句话描述累的作用)
 * @date 2020/8/26 16:55
 */
public class NacosServerList extends com.alibaba.cloud.nacos.ribbon.NacosServerList {

    public NacosServerList(NacosDiscoveryProperties discoveryProperties) {
        super(discoveryProperties);
    }

    public NacosServerList(IClientConfig config, NacosDiscoveryProperties discoveryProperties) {
        super(discoveryProperties);
        this.initWithNiwsConfig(config);
    }

    @Override
    public List<NacosServer> getInitialListOfServers() {
        return this.getServers();
    }

    @Override
    public List<NacosServer> getUpdatedListOfServers() {
        return this.getServers();
    }

    private List<NacosServer> getServers() {
        Object nacosServers = new ArrayList();

        try {
            NacosDiscoveryProperties discoveryProperties = this.getDiscoveryProperties();
            if (discoveryProperties != null) {
                String group = this.getDiscoveryProperties().getGroup();
                String serviceId = this.getServiceCurrentId();
                if (StringUtils.isNotBlank(serviceId)) {
                    List<Instance> instances = discoveryProperties.namingServiceInstance().selectInstances(serviceId, serviceId, true);
                    if (CollectionUtils.isEmpty(instances) && !serviceId.equals(group)) {
                        instances = discoveryProperties.namingServiceInstance().selectInstances(serviceId, group, true);
                    }

                    nacosServers = this.instancesToServerList(instances);
                }
            }

            return (List)nacosServers;
        } catch (Exception var6) {
            throw new IllegalStateException("Can not get service instances from nacos, serviceId=" + this.getServiceCurrentId(), var6);
        }
    }

    private NacosDiscoveryProperties getDiscoveryProperties() {
        NacosDiscoveryProperties discoveryProperties = (NacosDiscoveryProperties)ReflectHelper.getFieldValue(this, "discoveryProperties");
        return discoveryProperties;
    }

    private String getServiceCurrentId() {
        String serviceId = (String)ReflectHelper.getFieldValue(this, "serviceId");
        return serviceId;
    }

    private List<NacosServer> instancesToServerList(List<Instance> instances) {
        List<NacosServer> result = new ArrayList();
        if (com.alibaba.nacos.client.naming.utils.CollectionUtils.isEmpty(instances)) {
            return result;
        } else {
            Iterator var3 = instances.iterator();

            while(var3.hasNext()) {
                Instance instance = (Instance)var3.next();
                result.add(new NacosServer(instance));
            }

            return result;
        }
    }
}

